﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineProjectCommand.Commands;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineProjectCommand.CommandHandlers
{
    public class AllowExamineProjectCommandHandler : IRequestHandler<AllowExamineProjectCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<ExamineProject> _examineprojectRepository;
        public AllowExamineProjectCommandHandler(IBaseRepository<ExamineProject> examineprojectRepository)
        {
            _examineprojectRepository = examineprojectRepository;
        }
        public async Task<APIRequst<int>> Handle(AllowExamineProjectCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();
            var listarray = request.projectarray.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            if (listarray == null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
                return await Task.FromResult(result);
            }
            var listcode = _examineprojectRepository.GetAll().Where(x => listarray.Contains(x.ExamineProjectId)).ToList();
            foreach (var item in listcode)
            {
                item.isDel = true;
            }
           var  code= await _examineprojectRepository.UpdateRange(listcode);
            if(code>0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();

            }
            return await Task.FromResult(result);


        }
    }
}
